package org.edu.controller.manage;

import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import org.edu.bean.TmCompinfo;
import org.edu.common.PageView;
import org.edu.common.QueryResult;
import org.edu.controller.BaseAction;
import org.edu.service.manage.CompManageService;

/**
 * 
 * @author 杨霞 
 * 
 * 企业基本信息数据维护控制类 
 * 
 * 2011-4-25
 */
@SuppressWarnings("serial")
public class CompManageController extends BaseAction {
	@Resource
	private CompManageService service;
	private TmCompinfo entity;
	
	public CompManageService getService() {
		return service;
	}

	public void setService(CompManageService service) {
		this.service = service;
	}

	public TmCompinfo getEntity() {
		return entity;
	}

	public void setEntity(TmCompinfo entity) {
		this.entity = entity;
	}

	/**
	 * 分页显示所有的企业基本信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		PageView<TmCompinfo> pageView = new PageView<TmCompinfo>(20, page);
		QueryResult<TmCompinfo> queryResult = service.getQueryResult(pageView
				.getFirstResult(), pageView.getMaxresult());
		pageView.setQueryResult(queryResult);
		ServletActionContext.getRequest().setAttribute("pageView", pageView);
		return SUCCESS;
	}

	public String findCompinfo() throws Exception {
		PageView<TmCompinfo> pageView = new PageView<TmCompinfo>(20, page);
		QueryResult<TmCompinfo> queryResult = new QueryResult<TmCompinfo>();
		Object[] prams = new Object[1];
		if (conditionValue != null && !"".equals(conditionValue.trim())) {
			prams[0] = "%" + conditionValue.trim() + "%";
			String hqlCondition="";
			if (condition.equals("按企业名称查询")) {
				hqlCondition="o.taxplayer like ?";
			} else if (condition.equals("按企业识别码查询")) {
				hqlCondition="o.taxcode like ?";
			} 
			queryResult = service.getQueryResult(pageView.getFirstResult(),
					pageView.getMaxresult(), hqlCondition, prams);
			if (queryResult.getTotalrecord() ==0) {
				addActionMessage("系统不存在此类信息，请检查查询条件！");
				return "message";
			} else {
				
				pageView.setQueryResult(queryResult);
				ServletActionContext.getRequest().setAttribute("pageView",
						pageView);
				return SUCCESS;
			}
		} else {
			addActionMessage("查询条件不能为空！");
			return "message";
		}
	}

	/**
	 * 跳转到企业基本信息修改页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String forwardToUpdate() throws Exception {
		entity = service.find(entity.getId());
		return "forward";
	}

	/**
	 * 修改企业基本信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {

		try {
			service.updateComp(entity);
			addActionMessage("修改企业【" + entity.getTaxplayer() + "】的基本信息成功！");

		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("修改企业【" + entity.getTaxplayer() + "】的基本信息失败！"+e.getMessage());
		}

		return "message";
	}

	/**
	 * 删除企业基本信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		try {
			System.out.println(entity.getId());
			entity = service.find(entity.getId());
			service.deleteComp(entity);
			addActionMessage("删除企业【" + entity.getTaxplayer() + "】的基本信息成功！");
		} catch (Exception e) {
			addActionMessage("删除企业【" + entity.getTaxplayer() + "】的基本信息失败！");
		}
		return "message";
	}
	/**
	 * 企业基本详细信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String show() throws Exception {
		System.out.println(entity.getId());
		entity = service.find(entity.getId());
		return "show";
	}
}
